﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VNWIKI_WEB.DAO
{
    public class HomeDAO
    {
        //Lấy danh sách thể loại
        public List<Category> GetCatList()
        {
            List<Category> catList;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            catList = (from c in dt.Categories
                       where c.Deleted == false
                       select c).ToList();
            return catList;
        }

        //Lấy thể loại theo mã
        public Category GetCatByID(int catID)
        {
            Category cat;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            cat= (from c in dt.Categories
                       where c.ID.Equals(catID) && c.Deleted == false
                       select c).FirstOrDefault();
            return cat;
        }

        //Lấy danh sách album theo mã thể loại
        public List<Album> GetAlbumList(string catName)
        {
            List<Album> albumList;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            albumList = (from c in dt.Albums
                         where c.Category.Name.Equals(catName) && c.Deleted == false
                         select c).ToList();
            return albumList;
        }

        //Lấy danh sách album theo mã id
        public List<Album> GetAlbumListByID(int catID)
        {
            List<Album> albumList;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            albumList = (from c in dt.Albums
                         where c.Category.ID.Equals(catID) && c.Deleted == false
                         select c).ToList();
            return albumList;
        }

        //Lấy danh sách album theo mã id
        public List<Album> GetAlbumListByUserID(int userID)
        {
            List<Album> albumList;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            albumList = (from c in dt.Albums
                         where c.UserID.Equals(userID) && c.Deleted == false
                         select c).ToList();
            return albumList;
        }

        //Lấy album vừa mới insert
        public Album GetAlbumNewInsert(int userID)
        {
            Album album;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            album = (from c in dt.Albums
                         where c.UserID.Equals(userID) && c.Deleted == false
                         orderby c.ID descending
                         select c).FirstOrDefault();
            return album;
        }

        //Lấy danh sách album mới nhất
        public List<Album> GetAlbumList()
        {
            List<Album> albumList;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            albumList = (from c in dt.Albums
                         where c.Deleted == false
                         orderby c.ID descending
                         select c).ToList();
            return albumList;
        }

        //Lấy album theo ID
        public Album GetAlbumByID(int albumID)
        {
            Album album;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            album = (from c in dt.Albums
                         where c.ID.Equals(albumID) && c.Deleted == false
                         select c).FirstOrDefault();
            return album;
        }

        //Lấy album theo Name
        public Album GetAlbumByName(string albumName)
        {
            Album album;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            album = (from c in dt.Albums
                     where c.Name.Equals(albumName) && c.Deleted == false
                     select c).FirstOrDefault();
            return album;
        }

        //Lấy danh sách hình ảnh trong 1 album
        public List<Photo> GetPhotoList(int albumID)
        {
            List<Photo> photoList;
            VNWIKIDataContext dt = new VNWIKIDataContext();
            photoList = (from c in dt.Photos
                         where c.AlbumID.Equals(albumID) && c.Deleted == false
                         select c).ToList();
            return photoList;
        }

        //Lấy user theo ID
        public User GetUserByID(int userID)
        {
            VNWIKIDataContext dt = new VNWIKIDataContext();
            User user = (from u in dt.Users
                         where u.ID.Equals(userID) && u.Deleted == false
                         select u).FirstOrDefault();
            return user;
        }

        //Lấy danh sách Tags
        public List<string> GetListTags(string tagsInput)
        {
            List<string> tagsRS = new List<string>();
            string[] tagsList = tagsInput.Split(',');
            foreach (var pt in tagsList)
            {
                tagsRS.Add(pt.Trim());
            }
            return tagsRS;
        }

        //Tìm kiếm
        public List<Album> Search(string key)
        {
            VNWIKIDataContext dt = new VNWIKIDataContext();
            List<Album> albumList = (from c in dt.Albums
                                     where c.Name.Contains(key) && c.Deleted == false || c.Tags.Contains(key) && c.Deleted == false
                                     select c).ToList();
            return albumList;
        }


        //Update lượt xem album
        public void UpdatingViews(int albumID)
        {
            VNWIKIDataContext dt = new VNWIKIDataContext();
            var album = (from u in dt.Albums
                        where u.ID.Equals(albumID) && u.Deleted == false
                        select u).FirstOrDefault();
            album.Views = album.Views + 1;
            dt.SubmitChanges();
        }
    }
}